$(function(){
    //查询
    $('#serch_user').click(function(){
        let username=$('#sh_username').val()
        let id = $('#sh_id').val()
        let email =$('#sh_email').val()
        search_user(username,id,email)
    })
    function search_user(name,id,email){
        let table=$('#sh_result_list')
        //从服务器获取数据
        $.ajax({
            type:"GET",
            url:"http://localhost:7300/mock/5ee7242cb1a45736b4181f4a/dog/userlist2",
            success:function(msg){
                let result = screen_user(msg.data)
                take_user(result)
            },
            error:function(error){
                console.log(error.responseText);
            }
        })

        // 对数据进行筛选
        function screen_user(data){
            let user_list1=[]
            for(i in data){
                // console.log(data[i]);
                if(name!=""&&id==""&&email==""){
                    if(data[i].username.indexOf(name)!=-1){
                        user_list1.push(data[i])
                    }
                }
                if(name==""&&id!=""&&email==""){
                    if(data[i].id==id){
                        user_list1.push(data[i])
                    }
                }
                if(name==""&&id==""&&email!=""){
                    if(data[i].email.indexOf(email)!=-1){
                        user_list1.push(data[i])
                    }
                }
                if(name!=""&&id==""&&email!=""){
                    if(data[i].username.indexOf(name)!=-1 && data[i].email.indexOf(email)!=-1){
                        user_list1.push(data[i])
                    }
                }
                if(name==""&&id!=""&&email!=""){
                    if(data[i].id==id && data[i].email.indexOf(email)!=-1){
                        user_list1.push(data[i])
                    }
                }
                if(name!=""&&id!=""&&email==""){
                    if(data[i].username.indexOf(name)!=-1 && id==data[i].id){
                        user_list1.push(data[i])
                    }
                }
                if(name!=""&&id!=""&&email!=""){
                    if(data[i].username.indexOf(name)!=-1 && id==data[i].id&&data[i].email.indexOf(email)!=-1){
                        user_list1.push(data[i])
                    }
                }
                if(name==""&&id==""&&email==""){
                        user_list1.push(data[i])
                }
            }
            return user_list1
        }
     
        // 对数据进行显示
        function take_user(list){
            table.html("")
           for(i in list){
            table.append("<tr>"
            +"<td>"+list[i].id+"</td>"
            +"<td>"+list[i].username+"</td>"
            +"<td>"+list[i].email+"</td>"
            +"<td>"
               + "<div class='dropdown'>"
                   + "<button type='button' class='btn btn-dark dropdown-toggle' data-toggle='dropdown'>操作</button>"
                    +"<div class='dropdown-menu'>"
                        +"<a class='dropdown-item' href='#'>删除</a>"
                        +"<a class='dropdown-item' href='#''>修改</a>"
                    +"</div></div></td></tr>"
                    )
           }
        }
    }
   
    
})